package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.DataModelType;
import com.etone.smartAudit.dto.dataModelType.DataModelTypeDTO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.DataModelTypeMapper;
import com.etone.smartAudit.mapstruct.DataModelTypeMapStruct;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.dataModelType.DataModelTypeTreeVO;
import com.etone.smartAudit.vo.dataModelType.DataModelTypeVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: sovy
 * @date: 2020/11/5
 * @description:
 */

@Service
public class DataModelTypeService extends ServiceImpl<DataModelTypeMapper, DataModelType> {

    @Autowired
    private DataModelTypeMapStruct dataModelTypeMapStruct;

    /**
     * 数据模型分类树形结构
     *
     * @return
     */
    public List<DataModelTypeTreeVO> listTree() {
        List<DataModelTypeTreeVO> listTree = baseMapper.findListTree(null);
        return parseTree(0, 1, listTree);
    }

    /**
     * 递归解析成树形结构
     *
     * @param pid
     * @param sourceDatas
     * @return
     */
    public List<DataModelTypeTreeVO> parseTree(int pid, int level, List<DataModelTypeTreeVO> sourceDatas) {
        List<DataModelTypeTreeVO> dataModelTypeTreeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sourceDatas)) {
            sourceDatas.forEach(sourceData -> {
                if (sourceData.getPid().intValue() == pid) {
                    sourceData.setLevel(level);
                    dataModelTypeTreeVOS.add(sourceData);
                    sourceData.setChildren(parseTree(sourceData.getId(), level + 1, sourceDatas));
                }
            });
        }
        return dataModelTypeTreeVOS;
    }

    /**
     * 列表
     *
     * @param pid
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<DataModelTypeVO> list(Integer pid, int page, int pageSize) {
        Page<DataModelTypeVO> dataModelTypeVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> baseMapper.findList(pid));
        return PagerVO.of(dataModelTypeVOPage);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    public DataModelTypeVO detail(Integer id) {
        DataModelType dataModelType = get(id);
        return dataModelTypeMapStruct.getDataModelTypeVO(dataModelType);
    }

    /**
     * 详情
     *
     * @param id
     */
    public DataModelType get(Integer id) {
        DataModelType dataModelType = getById(id);
        if (dataModelType == null) {
            throw new CustomParameterizedException("数据模型分类不存在");
        }
        return dataModelType;
    }

    /**
     * 新增|编辑
     *
     * @param id
     * @param dataModelTypeDTO
     */
    public void save(Integer id, DataModelTypeDTO dataModelTypeDTO) {
        DataModelType dataModelType;
        if (id == null) {
            dataModelType = new DataModelType();
        } else {
            dataModelType = get(id);
        }
        dataModelTypeMapStruct.copyToDataModelType(dataModelType, dataModelTypeDTO);
        saveOrUpdate(dataModelType);
    }
}
